package com.songcan.gateway.utils;

import cn.hutool.core.util.StrUtil;
import com.songcan.gateway.exception.ValidatedExceotion;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.MultiValueMap;

import java.util.Map;
import java.util.Objects;

import static com.songcan.common.contans.CoreContants.MOBILE_CODE;
import static com.songcan.gateway.utils.GatewayContants.*;

public class ValidatedUtil {



    public static  void imageValidated(MultiValueMap multiValueMap, Map<String, String> codeParams, RedisTemplate redisTemplate,String type) throws ValidatedExceotion {
        switch (type){
            case TYPE_MOBILE:{
                String smsCode = codeParams.get("smsCode");
                String mobile = codeParams.get("mobile");
                if(StrUtil.isEmpty(mobile)){
                    throw new ValidatedExceotion("请输入手机号");
                }
                String redisCode = (String) redisTemplate.opsForValue().get(MOBILE_CODE+":"+mobile);
                if(StrUtil.isEmpty(redisCode)){
                    throw new ValidatedExceotion("验证码已经过期,请重新获取");
                }
                if(!smsCode.equals(redisCode)){
                    throw new ValidatedExceotion("验证码错误，请重新获取");
                }
                redisTemplate.delete(MOBILE_CODE+":"+mobile);
            }
            break;
            case TYPE_WECHAT:{
                String weChatCode = codeParams.get("weChatCode");
                if(StrUtil.isEmpty(weChatCode)){
                    throw new ValidatedExceotion("微信code不能为空");
                }
            }
            break;
            case TYPE_ALIYUN:{
                String mobileKeyToken = codeParams.get("mobileKeyToken");
                if(StrUtil.isEmpty(mobileKeyToken)){
                    throw new ValidatedExceotion("token不能为空");
                }
            }
            break;
            default:{
                if(multiValueMap.keySet().size() == 0){
                    throw new ValidatedExceotion("登录参数不能为空");
                }
//
//                String random = multiValueMap.getFirst("random").toString();
//                String imageCode = (String) redisTemplate.opsForValue().get("rand:"+random);
//                if(Objects.isNull(imageCode)){
//                    throw new ValidatedExceotion("验证码已经过期,请重新获取");
//                }
//                String code = codeParams.get("code");
//                if(Objects.isNull(code)){
//                    throw new ValidatedExceotion("请输入验证码");
//                }
//                if("1234".equals(code)){
//
//                }else {
//                    if (!imageCode.equalsIgnoreCase(code)) {
//                        throw new ValidatedExceotion("验证码不正确");
//                    }
//                }
//                redisTemplate.delete("rand"+random);
            }
            break;
        }

    }


}
